package cn.com.fattiger.framework.threadlocal;

/**
 * 
 * @author fattiger.xiaoyang
 * @date 2016/01/22
 * @qqGroup 48450434
 */
public class ThreadLocalDemo {
	private static ThreadLocal<Integer> idLocal = new ThreadLocal<Integer>();
	private static ThreadLocal<String> nameLocal = new ThreadLocal<String>();
	
	public static int getId() {
		return idLocal.get();
	}
	public static void setId(int id) {
		idLocal.set(id);
	}
	public static String getName() {
		return nameLocal.get();
	}
	public static void setName(String name) {
		nameLocal.set(name);
	}
	
	public static void main(String[] args) {
		for(int i = 0; i < 5; i++){
			new Thread(new Lunch()).start();
		}
	}
}

class Lunch implements Runnable{
	
	@Override
	public void run() {
		System.out.println("线程："+ Thread.currentThread().getName() + " 开始值: "+ThreadLocalDemo.getName());
		ThreadLocalDemo.setName(Thread.currentThread().getName());
		test1();
		try {
			Thread.sleep(1000*10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		test2();
		System.out.println("线程："+ Thread.currentThread().getName() + " 设定后: "+ThreadLocalDemo.getName());
	}
	
	public void test1(){
		System.out.println("test1 线程："+ Thread.currentThread().getName() + " 设定后: " + ThreadLocalDemo.getName());
	}
	
	public void test2(){
		System.out.println("test2 线程："+ Thread.currentThread().getName() + " 设定后: " + ThreadLocalDemo.getName());
	}
}